﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using EmotionRecognitionEntities;
using System.Diagnostics;
namespace EmotionRecognitionManager
{
    public static class Utilities
    {
        public static ReplyMsg SaveImageCapture(System.Drawing.Image image, string savePath, string fileName)
        {//
            try
            {
                FileStream fstream = new FileStream(savePath + fileName, FileMode.Create);
                image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
                fstream.Close();
                return new ReplyMsg(true, fileName);
            }
            catch (Exception)
            {
                return new ReplyMsg(false);
            }
        }

        public static ReplyMsg CopyFile(string sourcePath, string targetPath, string fileName, string newfileName) //path\sourceName -> path\destName
        {
            // To move a file or folder to a new location:
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, newfileName);

            // To copy a file to another location and  
            // overwrite the destination file if it already exists.
            System.IO.File.Copy(sourceFile, destFile, true);
            return new ReplyMsg(true, destFile);
        }
        public static ReplyMsg CopyFile(string fullSourcePath, string targetPath, string newFileName) //path\sourceName -> path\destName
        {
            // To move a file or folder to a new location:
            string sourceFile = System.IO.Path.GetFullPath(fullSourcePath);
            string destFile = System.IO.Path.Combine(targetPath, newFileName);

            // To copy a file to another location and  
            // overwrite the destination file if it already exists.
            System.IO.File.Copy(sourceFile, destFile, true);
            return new ReplyMsg(true, destFile);
        }
        public static ReplyMsg DeleteFile(string targetPath, string fileName) //path\sourceName -> path\destName
        {
            // Delete a file by using File class static method... 
            if (System.IO.File.Exists(targetPath + "\\" + fileName))
            {
                // Use a try block to catch IOExceptions, to 
                // handle the case of the file already being 
                // opened by another process. 
                try
                {
                    System.IO.File.Delete(@"C:\Users\Public\DeleteTest\test.txt");
                    return new ReplyMsg(true);
                }
                catch (System.IO.IOException e)
                {
                    Console.WriteLine(e.Message);
                    return new ReplyMsg(false);
                }
            }
            return new ReplyMsg(false);
        }
        public static ReplyMsg DeleteFile(string fullTargetPath) //path\sourceName -> path\destName
        {
            // Delete a file by using File class static method... 
            if (System.IO.File.Exists(fullTargetPath))
            {
                // Use a try block to catch IOExceptions, to 
                // handle the case of the file already being 
                // opened by another process. 
                try
                {
                    System.IO.File.Delete(@"C:\Users\Public\DeleteTest\test.txt");
                    return new ReplyMsg(true);
                }
                catch (System.IO.IOException e)
                {
                    Console.WriteLine(e.Message);
                    return new ReplyMsg(false);
                }
            }
            return new ReplyMsg(false);
        }

        public static ReplyMsg OpenFile(string fileName, string path)
        {
            try
            {
                Process.Start(path + "\\" + fileName);
                return new ReplyMsg(true);

            }
            catch (Exception)
            {
                return new ReplyMsg(false);
            }
        }
    }
}
